package com.idata.service.builder;

import com.alibaba.fastjson2.JSONObject;
import com.idata.model.dto.DisputeEntityBuilderDto;
import com.idata.model.entity.DisputeEntity;
import com.idata.model.entity.DisputeEntity.People;
import com.idata.model.enums.DisputeSourceEnum;
import com.idata.model.proxyDO.KGBaseDisputeInfoDO;
import com.idata.service.utils.HighRiskDisputeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DisputeEntityBuilder {

    private static final Logger LOGGER = LoggerFactory.getLogger(DisputeEntityBuilder.class);

    /**
     * 将 j yd 的函数复制到builder中
     * 将 API 响应的数据解析成 GraphAnalysisResponse
     * @param jsonObject 响应
     * @return 人事纠纷事件
     */
    public static DisputeEntityBuilderDto convertFromJsonObject(JSONObject jsonObject) {
        DisputeEntityBuilderDto result = new DisputeEntityBuilderDto();
        DisputeEntity disputeEntity = new DisputeEntity();
        List<JSONObject> caseList = jsonObject.getList("data", JSONObject.class);

        for (JSONObject caseJsonObject : caseList) {
            String caseName = caseJsonObject.get("caseName").toString();
            DisputeSourceEnum disputeSourceEnum = DisputeSourceEnum.fromDesc(caseName);
            if (disputeSourceEnum == null) {
                // 知识图谱返回的数据没有和定义的枚举对应上
                continue;
            }
            KGBaseDisputeInfoDO kgBaseDisputeInfoDO = caseJsonObject.toJavaObject(KGBaseDisputeInfoDO.class);
            String disputeId = kgBaseDisputeInfoDO.getDisputeId();
//            removeCaseEdgeInfo(caseJsonObject);
            switch (disputeSourceEnum) {
                case POLICE_110:
                    // todo 这里可以将caseJsonObject反序列化成javabean，然后使用HasHotWordContext接口的能力
                    String policeResponse = (String) caseJsonObject.get("110警情.报警内容");
                    String alarmContent = (String) caseJsonObject.get("110警情.出警情况");
                    String context = policeResponse + alarmContent;
                    // 高危预警
                    boolean policeMatchInHighRisk = HighRiskDisputeUtil.isHighRiskDispute(context);
                    if (policeMatchInHighRisk) {
                        disputeEntity.getDisputeSourceMap().computeIfAbsent(DisputeSourceEnum.HIGH_RISK.getCode(), k -> new ArrayList<>()).add(disputeId);
                    }
                    // 这里切记，塞进去的是caseJsonObject，不能是java bean
                    result.getDisputeContentMap().computeIfAbsent(DisputeSourceEnum.POLICE_110.getCode(), k -> new HashMap<>()).put(disputeId, caseJsonObject);
                    disputeEntity.getDisputeSourceMap().computeIfAbsent(DisputeSourceEnum.POLICE_110.getCode(), k -> new ArrayList<>()).add(disputeId);
                    break;
                case COLLABORATIVE_CASE:
                    // 高危预警
                    String caseContext = (String) caseJsonObject.get("协同办案.简要案情");
                    boolean cseMatchInHighRisk = HighRiskDisputeUtil.isHighRiskDispute(caseContext);
                    if (cseMatchInHighRisk) {
                        disputeEntity.getDisputeSourceMap().computeIfAbsent(DisputeSourceEnum.HIGH_RISK.getCode(), k -> new ArrayList<>()).add(disputeId);
                    }
                    disputeEntity.getDisputeSourceMap().computeIfAbsent(DisputeSourceEnum.COLLABORATIVE_CASE.getCode(), k -> new ArrayList<>()).add(disputeId);
                    result.getDisputeContentMap().computeIfAbsent(DisputeSourceEnum.COLLABORATIVE_CASE.getCode(), k -> new HashMap<>()).put(disputeId, caseJsonObject);
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + disputeSourceEnum);
            }
            Object disputePersonListObject = caseJsonObject.get("disputePersonList");
            Map<String, String> disputePersonMap = null;
            try {
                disputePersonMap = (Map<String, String>) disputePersonListObject;
            } catch (Exception e) {
                LOGGER.error("从jsonObject中提取disputePersonList时出错，", e);
                disputePersonMap = new HashMap<>();
            }

            for (Map.Entry<String, String> disputePerson : disputePersonMap.entrySet()) {
                People people = new People(disputePerson.getValue(), disputePerson.getKey());
                disputeEntity.getPeopleDisputeMap().computeIfAbsent(people, k -> new ArrayList<>()).add(disputeId);
            }

            disputeEntity.getAssociatedTypeMap().computeIfAbsent(kgBaseDisputeInfoDO.getRelationType(), k -> new ArrayList<>()).add(disputeId);
            disputeEntity.getDisputeIdList().add(disputeId);
        }
        result.setDisputeEntity(disputeEntity);
        return result;
    }



    public static void removeCaseEdgeInfo(JSONObject object) {
        object.remove("caseName");
        object.remove("关联值");
        object.remove("纠纷关系ID");
        object.remove("关联类型");
    }
}
